package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.kejiguan.ui.R;
import com.kejiguan.ui.adapter.SheduleObjectAdapter;
import com.kejiguan.ui.adapter.SheduleOperationAdapter;
import com.kejiguan.ui.adapter.SheduleTimeTypeAdapter;
import com.kejiguan.ui.adapter.SheduleWeekAdapter;
import com.kejiguan.ui.adapter.TimeSpinnerAdapter;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.model.Shedule;
import com.kejiguan.ui.model.SheduleActionType;
import com.kejiguan.ui.model.SheduleTypeModel;
import com.kejiguan.ui.model.WeekItem;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 16/5/13.
 */
public class ModifySheduleDialog extends Dialog implements View.OnClickListener{


    private View mDialogView;
    private EditText sheduleNameInput;
    private Spinner sheduleTypeSpinner;
    private Spinner hourSpinner,minSpinner,secSpinner;
    private Spinner sheduleActionSpinner;
    private Spinner sheduleObjectSpinner;
    private Spinner weekSpinner;

    private View addBtn,cancelBtn;
    private ModifySheduleListerner modifySheduleListerner;
    private Shedule preShedule;
    private List<ExhibitionNeItem> exhibitionNeItems;

    public ModifySheduleDialog(Context context) {
        super(context);
        init(context);
    }

    public ModifySheduleDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected ModifySheduleDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    public interface ModifySheduleListerner
    {
        public void modifyShedule(Shedule shedule);
    }


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_add_shedule_layout, null);

        sheduleNameInput= (EditText) mDialogView.findViewById(R.id.shedule_name_input);
        sheduleTypeSpinner= (Spinner) mDialogView.findViewById(R.id.type_select);
        weekSpinner= (Spinner) mDialogView.findViewById(R.id.week_select);

        hourSpinner= (Spinner) mDialogView.findViewById(R.id.hour_select);
        minSpinner= (Spinner) mDialogView.findViewById(R.id.min_select);
        secSpinner= (Spinner) mDialogView.findViewById(R.id.sec_select);

        sheduleActionSpinner= (Spinner) mDialogView.findViewById(R.id.action_select);
        sheduleObjectSpinner= (Spinner) mDialogView.findViewById(R.id.object_select);

        addBtn=mDialogView.findViewById(R.id.add_shedule_confirm_btn);
        cancelBtn=mDialogView.findViewById(R.id.add_shedule_cancel_btn);
        addBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        hourSpinner.setAdapter(new TimeSpinnerAdapter(context,0,23));
        minSpinner.setAdapter(new TimeSpinnerAdapter(context,0,59));
        secSpinner.setAdapter(new TimeSpinnerAdapter(context,0,59));
        sheduleActionSpinner.setAdapter(new SheduleOperationAdapter(getContext(), CommonDate.actionTypes));
        weekSpinner.setAdapter(new SheduleWeekAdapter(getContext(), CommonDate.weekItems));
        sheduleTypeSpinner.setAdapter(new SheduleTimeTypeAdapter(getContext(), CommonDate.sheduleTypeModels));
        sheduleTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                Log.e(AddSheduleDialog.class.getSimpleName(), "position:" + position);
                if (position == (sheduleTypeSpinner.getAdapter().getCount() - 1)) {
                    weekSpinner.setVisibility(View.VISIBLE);
                } else {
                    weekSpinner.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        exhibitionNeItems =new ArrayList<>();
        ExhibitionNeItem exhibitionNeItem =new ExhibitionNeItem();
        exhibitionNeItem.setExhibitionId("TALL");
        exhibitionNeItem.setExhibitionName("所有");
        exhibitionNeItems.add(exhibitionNeItem);
        List<DisplayArea> displayAreas= CacheDataUtil.getInstance().getDisplayAreas();

        for (int i = 0; i < displayAreas.size(); i++) {
            DisplayArea displayArea = displayAreas.get(i);
            exhibitionNeItems.add(new ExhibitionNeItem(displayArea.getDisplayId(),displayArea.getDisplayId()+displayArea.getDisplayName()));
            exhibitionNeItems.addAll(displayArea.getExhibitionNeItems());
        }

        sheduleObjectSpinner.setAdapter(new SheduleObjectAdapter(getContext(), exhibitionNeItems));
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.add_shedule_cancel_btn:
                dismiss();
                break;
            case R.id.add_shedule_confirm_btn:

                if (null!=modifySheduleListerner)
                {
                    String sheduleName=sheduleNameInput.getText().toString();
                    if (TextUtils.isEmpty(sheduleName))
                    {
                        Toast.makeText(getContext(),"任务名不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }

                    SheduleTimeTypeAdapter sheduleTimeTypeAdapter= (SheduleTimeTypeAdapter) sheduleTypeSpinner.getAdapter();
                    String timeType=((SheduleTypeModel) sheduleTimeTypeAdapter.getItem(sheduleTypeSpinner.getSelectedItemPosition())).getTypeId();
                    if ("5".equals(timeType))
                    {
                        SheduleWeekAdapter weekAdapter= (SheduleWeekAdapter) weekSpinner.getAdapter();
                        String weekType=((WeekItem)weekAdapter.getItem(weekSpinner.getSelectedItemPosition())).getWeekId();
                        timeType=timeType+","+weekType;
                    }

                    SheduleOperationAdapter operationAdapter= (SheduleOperationAdapter) sheduleActionSpinner.getAdapter();
                    String actionType=((SheduleActionType)operationAdapter.getItem(sheduleActionSpinner.getSelectedItemPosition())).getActionId();

                    SheduleObjectAdapter sheduleObjectAdapter= (SheduleObjectAdapter) sheduleObjectSpinner.getAdapter();
                    String actionObjerct=((ExhibitionNeItem)sheduleObjectAdapter.getItem(sheduleObjectSpinner.getSelectedItemPosition())).getExhibitionId();

                    int hour=hourSpinner.getSelectedItemPosition();
                    int min=minSpinner.getSelectedItemPosition();
                    int sec=secSpinner.getSelectedItemPosition();

                    String hourS=hour<10?"0"+hour:String.valueOf(hour);
                    String minS=min<10?"0"+min:String.valueOf(min);
                    String secS=sec<10?"0"+sec:String.valueOf(sec);

                    Shedule shedule=new Shedule();
                    shedule.setScheduleId(preShedule.getScheduleId());
                    shedule.setScheduleName(sheduleName);
                    shedule.setScheduleType(timeType);
                    shedule.setScheduleAction(actionType);
                    shedule.setScheduleObject(actionObjerct);
                    shedule.setScheduleNote("");
                    shedule.setScheduleState("0");
                    shedule.setScheduleTime(hourS + ":" + minS + ":" + secS);

                    modifySheduleListerner.modifyShedule(shedule);
                }
                dismiss();
                break;
        }
    }

    public void setModifySheduleListerner(ModifySheduleListerner modifySheduleListerner) {
        this.modifySheduleListerner = modifySheduleListerner;
    }

    public void setPreShedule(Shedule preShedule) {
        this.preShedule = preShedule;
        //任务名
        sheduleNameInput.setText(preShedule.getScheduleName());
        //任务动作
        String actionType=preShedule.getScheduleAction();
        if ("K".equals(actionType))
        {
            sheduleActionSpinner.setSelection(0);
        }else
        {
            sheduleActionSpinner.setSelection(1);
        }
        //任务周期
        String sheduleType=preShedule.getScheduleType();
        switch (sheduleType)
        {
            case "1":
                sheduleTypeSpinner.setSelection(0);
                break;
            case "2":
                sheduleTypeSpinner.setSelection(1);
                break;
            case "4":
                sheduleTypeSpinner.setSelection(2);
                break;
            default:
                String[] types = sheduleType.split(",");
                if (null != types && types.length == 2) {
                    sheduleTypeSpinner.setSelection(3);
                    switch (types[1]) {

                        case "MON":
                            weekSpinner.setSelection(0);
                            break;
                        case "TUE":
                            weekSpinner.setSelection(1);
                            break;
                        case "WED":
                            weekSpinner.setSelection(2);
                            break;
                        case "THU":
                            weekSpinner.setSelection(3);
                            break;
                        case "FRI":
                            weekSpinner.setSelection(4);
                            break;
                        case "SAT":
                            weekSpinner.setSelection(5);
                            break;
                        case "SUN":
                            weekSpinner.setSelection(6);
                            break;
                        default:
                            weekSpinner.setSelection(0);
                            break;
                    }

                } else {
                    sheduleTypeSpinner.setSelection(0);
                }
                break;
        }

        //任务对象
        String sheduleObject=preShedule.getScheduleObject();
        for (int i = 0; i< exhibitionNeItems.size(); i++)
        {
            if (exhibitionNeItems.get(i).getExhibitionId().equals(sheduleObject))
            {
                sheduleObjectSpinner.setSelection(i);
                break;
            }
        }
        //时间
        String sheduleTime=preShedule.getScheduleTime();
        String[] times=sheduleTime.split(":");
        String horString=times[0];
        String minString=times[1];
        String secString=times[2];
        int hor=Integer.valueOf(horString);
        int min=Integer.valueOf(minString);
        int sec=Integer.valueOf(secString);
        hourSpinner.setSelection(hor);
        minSpinner.setSelection(min);
        secSpinner.setSelection(sec);
    }
}
